#ifndef __TIMER_H__
#define __TIMER_H__

#include "ch32v_def.h"
#include "PortNames.h"

#ifdef __cplusplus
extern "C"
{
#endif

#ifndef TIM_IRQ_PRIO
#define TIM_IRQ_PRIO 3
#endif

#ifndef TIM_IRQ_SUBPRIO
#define TIM_IRQ_SUBPRIO 0
#endif

	typedef enum
	{
#if defined(TIM1_BASE)
		TIMER1_INDEX,
#endif
#if defined(TIM2_BASE)
		TIMER2_INDEX,
#endif
#if defined(TIM3_BASE)
		TIMER3_INDEX,
#endif
#if defined(TIM4_BASE)
		TIMER4_INDEX,
#endif
#if defined(TIM5_BASE)
		TIMER5_INDEX,
#endif
#if defined(TIM6_BASE)
		TIMER6_INDEX,
#endif
#if defined(TIM7_BASE)
		TIMER7_INDEX,
#endif
#if defined(TIM8_BASE)
		TIMER8_INDEX,
#endif
#if defined(TIM9_BASE)
		TIMER9_INDEX,
#endif
#if defined(TIM10_BASE)
		TIMER10_INDEX,
#endif
#if defined(TIM11_BASE)
		TIMER11_INDEX,
#endif
#if defined(TIM12_BASE)
		TIMER12_INDEX,
#endif
#if defined(TIM13_BASE)
		TIMER13_INDEX,
#endif
#if defined(TIM14_BASE)
		TIMER14_INDEX,
#endif
#if defined(TIM15_BASE)
		TIMER15_INDEX,
#endif
#if defined(TIM16_BASE)
		TIMER16_INDEX,
#endif
#if defined(TIM17_BASE)
		TIMER17_INDEX,
#endif
#if defined(TIM18_BASE)
		TIMER18_INDEX,
#endif
#if defined(TIM19_BASE)
		TIMER19_INDEX,
#endif
#if defined(TIM20_BASE)
		TIMER20_INDEX,
#endif
#if defined(TIM21_BASE)
		TIMER21_INDEX,
#endif
#if defined(TIM22_BASE)
		TIMER22_INDEX,
#endif
		TIMER_NUM,
		UNKNOWN_TIMER = 0XFFFF
	} timer_index_t;

	typedef struct
	{
		void *__this;
		TIM_TypeDef *instance;
		timer_index_t idx;
		uint32_t preemptPriority;
		uint32_t subPriority;
	} timerObj_t;

	void enableTimerClock(timer_index_t idx);
	void disableTimerClock(timer_index_t idx);

	IRQn_Type getTimerIrq(TIM_TypeDef *tim);
	IRQn_Type getTimerCCIrq(TIM_TypeDef *tim);
	uint8_t getTimerClkSrc(TIM_TypeDef *tim);
#ifdef __cplusplus
}
#endif

#endif // __TIMER_H__